import { _decorator, Component, Node, Sprite, resources, SpriteFrame, ImageAsset } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PublicFn')
export class PublicFn extends Component {
    private static instance: PublicFn = null;
    static get Instance() {
        return this.instance ? this.instance : (this.instance = new PublicFn(), this.instance);
    }
    start() {

    }

    update(deltaTime: number) {

    }
    /**随机打乱数组函数 */
    UpsetArray<T>(arr: Array<T>): Array<T> {
        arr.sort(() => Math.random() - 0.5);
        return arr
    }
    /**
     * 更改精灵组件纹理
     * @param path ab包资源路径
     * @param sp 要更改的精灵组件
     */
    changeImg(path: String, sp: Sprite) {
        resources.load(`${path}/spriteFrame`, (e, b: SpriteFrame) => {
            if (e) {
                console.log('e', e);
            }
            else {
                sp.spriteFrame = b;
            }
        })
    }
    /**
     * 远程加载图片
     * @param url 图片路径
     * @param sprite 精灵组件
     */
    changeUrlImg(url:string,sprite:Sprite) {
        const image = new Image();
        image.src = url;
        image.onload = () => {
            const l = new ImageAsset(image);
            const sp = SpriteFrame.createWithImage(l);
            sprite.spriteFrame = sp;
        }
    }
}

